package com.usian.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.VO.Result;
import com.usian.pojo.*;
import com.usian.service.*;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.crypto.prng.EntropyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.handler.RequestMatchResult;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*
 * 作者：m鹏h
 * 时间；2023/8/23 16:39
 * 描述：
 **/
@RestController
@Slf4j
@RequestMapping("sy")
public class SyController {
    @Autowired
    private SyService syService;

    @Autowired
    private MemberSjService memberSjService;
    @Autowired
    private FeignCouService feignCouService;

    @Autowired
    private FeignSjService feignSjService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FeignTrainService feignTrainService;

    @Autowired
    private MemberCatalogueService memberCatalogueService;

    @Autowired
    private CouService couService;

    @Autowired
    private FeignCouTentService feignCouTentService;
    @Autowired
    private AnsService ansService;

    @Autowired
    private FeignQueService feignQueService;

    @GetMapping("findQueNiDeXuanZe")
    public Result findQueNiDeXuanZe(){
        String a = "";
        String b = "";
        List<Ans> list = ansService.list();
        List<String> collect = list.stream().map(ans -> {
            return ans.getQueAns();
        }).collect(Collectors.toList());

        String[] arr = collect.toArray(new String[collect.size()]);
        log.info("arr:{}",arr);

        String[] queZQDA = feignQueService.findQueZQDA();



        for (String s : queZQDA) {
          a = s;
        }
        for (String s2 : arr) {
         b= s2;
        }
        Ans ans = new Ans();

        if (b.equals(a)) ans.setStatus("1");







        return  Result.Success(ans);
    }

    @GetMapping("getyz")
    public Result getyz(){
        List<Ans> list = ansService.list();
        int size = list.size();
        return Result.Success(size);
    }

    @GetMapping("setAns")
    public Result setAns(Integer queId , String ops){
        LambdaQueryWrapper<Ans> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ans::getQueId, queId);
        Ans ans1 = ansService.getOne(wrapper);
        if (ans1==null){
            Ans ans = new Ans();
            ans.setQueAns(ops);
            ans.setQueId(queId);
            ansService.save(ans);
            return Result.Success("");
        }else {
            Ans ans = new Ans();
            ans.setId(ans1.getId());
            ans.setQueAns(ops);
            ans.setQueId(ans1.getQueId());
            ansService.updateById(ans);
            return null;
        }

    }
    @GetMapping("ndxz")
    public Result ndxz(Integer queId){
        LambdaQueryWrapper<Ans> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ans::getQueId,queId);
        Ans ans = ansService.getOne(wrapper);
        return Result.Success(ans.getQueAns());

    }


    @GetMapping("zqAns")
    public Result zqAns(Integer queId , String ops){
        LambdaQueryWrapper<Ans> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ans::getQueId, queId);
        Ans ans1 = ansService.getOne(wrapper);
        System.out.println(ans1);
        return Result.Success(ans1);

    }



    @GetMapping("CouList")
    public Result CouList(Cou cou){
        return Result.Success(feignCouService.couList(cou));
    }

    @GetMapping("getTsNum")
    public Result getTsNum(Integer sjId){
        return Result.Success(feignSjService.getTsNum(sjId));
    }
    @GetMapping("getTsNum2")
    public Result getTsNum2(Integer sjId){
        return Result.Success(feignSjService.getTsNum2(sjId));
    }

    @GetMapping("setAlreadyLearn")
    public Result setAlreadyLearn(Integer[] catalugueIds){
        String member = (String)redisTemplate.opsForValue().get("memberMessage");
        Member member1 = JSON.parseObject(member, Member.class);

        Integer couId = (Integer) redisTemplate.opsForValue().get("课程ID");


        LambdaQueryWrapper<MemberCatalogue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberCatalogue::getMemberId, member1.getId()).eq(MemberCatalogue::getCouId, couId);
        memberCatalogueService.remove(wrapper);

        for (Integer catalugueId : catalugueIds) {
            MemberCatalogue m = new MemberCatalogue();
            m.setMemberId(Integer.parseInt(member1.getId()+""));
            m.setCatalogueId(catalugueId);
            m.setCouId(couId);
            memberCatalogueService.save(m);
        }
        return Result.Success("恭喜完成这些任务，继续加油哦！");

    }

    @GetMapping("getSjWcrsNum")
    public Result getSjWcrsNum1(Integer sjId){
        log.info("试卷deID:{}",sjId);
        LambdaQueryWrapper<MemberSj> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberSj::getSjId,sjId);
        List<MemberSj> memberSjList = memberSjService.list(wrapper);
        int size = memberSjList.size();
       return Result.Success(size);
    }

    @GetMapping("getSjWcrsNum2")
    public Result getSjWcrsNum2(Integer sjId){
        log.info("试卷deID:{}",sjId);

        LambdaQueryWrapper<MemberSj> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberSj::getSjId,sjId);
        List<MemberSj> memberSjList = memberSjService.list(wrapper);
        int size = memberSjList.size();
       return Result.Success(size);
    }





    @GetMapping("sjSyList")
    public Result sjSyList(){
        return Result.Success(feignSjService.sjSyList());

    }

    @GetMapping("trainList")
    public Result trainList(){

        List<Train> list = feignTrainService.trainListSy();
        return Result.Success(list);

    }

    @GetMapping("getTrainCouNum")
    public Result getTrainCouNum(@RequestParam(value = "trainId") Integer trainId){
        return Result.Success(feignTrainService.getTrainCouNum(trainId));
    }

    @GetMapping("getTrainSjNum")
    public Result getTrainSjNum(@RequestParam(value = "trainId") Integer trainId){
        return Result.Success(feignTrainService.getTrainSjNum(trainId));
    }

    @GetMapping("getTrainCouNum2")
    public Result getTrainCouNum2(@RequestParam(value = "trainId") Integer trainId){
        return Result.Success(feignTrainService.getTrainCouNum(trainId));
    }

    @GetMapping("getTrainSjNum2")
    public Result getTrainSjNum2(@RequestParam(value = "trainId") Integer trainId){
        return Result.Success(feignTrainService.getTrainSjNum(trainId));
    }



    @GetMapping("getCouNum")
    public Result getCouNum(HttpServletRequest request){
//        String token = request.getHeader("token");
//        JWT jwt = JWTUtil.parseToken(token);
//        Object userId = jwt.getPayload("userId");
//        log.info("=============:{}",userId);


        String memberMessage = (String) redisTemplate.opsForValue().get("memberMessage");
        Member member = JSON.parseObject(memberMessage, Member.class);

        LambdaQueryWrapper<Sy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sy::getMemberId, member.getId());
        List<Sy> list = syService.list(wrapper);
        List<Integer> collect = list.stream().map(sy -> {
            return sy.getCouId();
        }).collect(Collectors.toList());
        ArrayList sies = new ArrayList<>();
        for (Integer integer : collect) {
            if (integer!=null){
                sies.add(integer);
            }
        }


        int i = sies.size();
        return Result.Success(i);
    }


    @GetMapping("syCou")
    public Result syCou(){
       List<Cou> list = couService.syCou();
        return Result.Success(list);
    }

    @GetMapping("syCouNew")
    public Result syCouNew(){
       List<Cou> list = couService.syCouNew();
        return Result.Success(list);
    }



    @GetMapping("dz1")
    public Result dz1(@RequestParam Integer couId){
        LambdaQueryWrapper<Cou> w = new LambdaQueryWrapper<>();
        w.eq(Cou::getId, couId);
        Cou cou = couService.getOne(w);
        String member = (String) redisTemplate.opsForValue().get("memberMessage");
        Member member1 = JSON.parseObject(member, Member.class);

        Integer o = (Integer) redisTemplate.opsForValue().get(couId+member1.getId());
        if (o==null){
            cou.setLikeNum(cou.getLikeNum()+1);
            redisTemplate.opsForValue().set(couId+member1.getId(), cou.getLikeNum());
            couService.updateById(cou);
            return Result.Success("感谢您的支持");
        }else {
            cou.setLikeNum(cou.getLikeNum()-1);
            redisTemplate.delete(couId+member1.getId());
            couService.updateById(cou);
            return Result.Success("是对我们有什么不满意的嘛,亲");
        }

    }

    @GetMapping("getCatalogueAltogether")
    public Result getCatalogueAltogether(){
        String member = (String)redisTemplate.opsForValue().get("memberMessage");
        Member member1 = JSON.parseObject(member, Member.class);
        Integer couId = (Integer) redisTemplate.opsForValue().get("课程ID");
        LambdaQueryWrapper<MemberCatalogue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberCatalogue::getMemberId,member1.getId()).eq(MemberCatalogue::getCouId,couId);
        List<MemberCatalogue> memberCatalogueList = memberCatalogueService.list(wrapper);
        int size = memberCatalogueList.size();
        return Result.Success(size);
    }



    @GetMapping("dz2")
    public Result dz2(@RequestParam Integer couId){
        LambdaQueryWrapper<Cou> w = new LambdaQueryWrapper<>();
        w.eq(Cou::getId, couId);
        Cou cou = couService.getOne(w);
        String member = (String) redisTemplate.opsForValue().get("memberMessage");
        Member member1 = JSON.parseObject(member, Member.class);
        Integer o = (Integer) redisTemplate.opsForValue().get(couId+member1.getId());
        if (o==null){
            cou.setLikeNum(cou.getLikeNum()+1);
            redisTemplate.opsForValue().set(couId+member1.getId(), cou.getLikeNum());
            couService.updateById(cou);
            return Result.Success("感谢您的支持");
        }else {
            cou.setLikeNum(cou.getLikeNum()-1);
            redisTemplate.delete(couId+member1.getId());
            couService.updateById(cou);
            return Result.Success("是对我们有什么不满意的嘛,亲");
        }


    }


    @GetMapping("dz3")
    public Result dz3(@RequestParam Integer couId){
        LambdaQueryWrapper<Cou> w = new LambdaQueryWrapper<>();
        w.eq(Cou::getId, couId);
        Cou cou = couService.getOne(w);
        String member = (String) redisTemplate.opsForValue().get("memberMessage");
        Member member1 = JSON.parseObject(member, Member.class);
        Integer o = (Integer) redisTemplate.opsForValue().get(couId+member1.getId());
        if (o==null){
            cou.setLikeNum(cou.getLikeNum()+1);
            redisTemplate.opsForValue().set(couId+member1.getId(), cou.getLikeNum());
            couService.updateById(cou);
            return Result.Success("感谢您的支持");
        }else {
            cou.setLikeNum(cou.getLikeNum()-1);
            redisTemplate.delete(couId+member1.getId());
            couService.updateById(cou);
            return Result.Success("是对我们有什么不满意的嘛,亲");
        }


    }


    @GetMapping("dz4")
    public Result dz4(@RequestParam Integer couId){
        LambdaQueryWrapper<Cou> w = new LambdaQueryWrapper<>();
        w.eq(Cou::getId, couId);
        Cou cou = couService.getOne(w);
        String member = (String) redisTemplate.opsForValue().get("memberMessage");
        Member member1 = JSON.parseObject(member, Member.class);
        Integer o = (Integer) redisTemplate.opsForValue().get(couId+member1.getId());
        if (o==null){
            cou.setLikeNum(cou.getLikeNum()+1);
            redisTemplate.opsForValue().set(couId+member1.getId(), cou.getLikeNum());
            couService.updateById(cou);
            return Result.Success("感谢您的支持");
        }else {
            cou.setLikeNum(cou.getLikeNum()-1);
            redisTemplate.delete(couId+member1.getId());
            couService.updateById(cou);
            return Result.Success("是对我们有什么不满意的嘛,亲");
        }


    }

    @GetMapping("getCompeNum")
    public Result getCompeNum(HttpServletRequest request){


        String memberMessage = (String) redisTemplate.opsForValue().get("memberMessage");
        Member member = JSON.parseObject(memberMessage, Member.class);

        LambdaQueryWrapper<Sy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sy::getMemberId, member.getId());
        List<Sy> list = syService.list(wrapper);
        List<Integer> collect = list.stream().map(sy -> {
            return sy.getConpe();
        }).collect(Collectors.toList());
        ArrayList sies = new ArrayList<>();
        for (Integer integer : collect) {
            if (integer!=null){
                sies.add(integer);
            }
        }


        int i = sies.size();
        return Result.Success(i);
    }

    @GetMapping("getTrainNum")
    public Result getTrainNum(){



        String memberMessage = (String) redisTemplate.opsForValue().get("memberMessage");
        Member member = JSON.parseObject(memberMessage, Member.class);

        LambdaQueryWrapper<Sy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sy::getMemberId, member.getId());
        List<Sy> list = syService.list(wrapper);
        List<Integer> collect = list.stream().map(sy -> {
            return sy.getTrainId();
        }).collect(Collectors.toList());
        ArrayList sies = new ArrayList<>();
        for (Integer integer : collect) {
            if (integer!=null){
                sies.add(integer);
            }
        }


        int i = sies.size();
        return Result.Success(i);
    }

    @GetMapping("findCoutent")
    public Result findCoutent(){
        return feignCouTentService.findCoutent();
    }

    @GetMapping("setRedisCouId")
    public Result setRedisCouId(Integer  couId){
        redisTemplate.opsForValue().set("课程ID",couId);
        return Result.Success("欢迎来到课程的目录页面");
    }


    @GetMapping("setRedisCompeId")
    public Result setRedisCompeId(Integer  compeId){
        redisTemplate.opsForValue().set("竞赛ID",compeId);
        return Result.Success("欢迎来到竞赛的的详情页面，快去报名吧！");
    }

    @GetMapping("alreadyLearn")
    public Result alreadyLearn(){
        String member = (String)redisTemplate.opsForValue().get("memberMessage");
        Member member1 = JSON.parseObject(member, Member.class);
        Integer couId = (Integer) redisTemplate.opsForValue().get("课程ID");
        LambdaQueryWrapper<MemberCatalogue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberCatalogue::getMemberId,member1.getId()).eq(MemberCatalogue::getCouId, couId);
        List<MemberCatalogue> memberCatalogueList = memberCatalogueService.list(wrapper);
        List<Integer> collect = memberCatalogueList.stream().map(memberCatalogue -> {
            return memberCatalogue.getCatalogueId();
        }).collect(Collectors.toList());
        return  Result.Success(collect);

    }
}
